<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
 
    /**
     * 使用数据库模型进行用户验证，是否验证成功，返回布尔值
     * @return booleanr 
     */
    public function authenticate()
    {
        $user = User::model()->findByAttributes(array('username' => $this->username));
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } 
        else {
            if ($user->password !== md5($this->password)) {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } else {
                $this->_id = $user->id;
                $user->last_login_time = time();
                $user->update();
                
                // 向Session中写入最后登录信息
                $this->setState('lastLoginTime', $user->last_login_time);
                $this->errorCode = self::ERROR_NONE;
            }
        }
        return!$this->errorCode;
    }
 
    public function getId() 
    {
        return $this->_id;
    }
}